package kr.ac.kaist.vrowser.controllers;

import java.sql.SQLException;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import android.speech.tts.TextToSpeech;
import android.util.Log;

import com.j256.ormlite.dao.Dao;

import kr.ac.kaist.vrowser.R;
import kr.ac.kaist.vrowser.application.VrowserApplication;
import kr.ac.kaist.vrowser.db.models.Bookmark;
import kr.ac.kaist.vrowser.getbookmark.GetBookmarkInterface;

public class BookmarkController extends BaseController {

	private Dao<Bookmark, Integer> bookmarkDao;
	private List<Bookmark> bookmarks = new ArrayList<Bookmark>();
	private GetBookmarkInterface bmkChange;
	
	//for double long press detection
	private int chosenBookmark = -1;
	private long doublePressTimeInterval = 5000;
	private long lastTouchedTime = 0;
	
	private int currentPosition = 0;

	public BookmarkController(VrowserApplication app) throws SQLException {
		super(app);
		
		bookmarkDao = app.getDatabaseHelper().getBookmarkDao();
		
		//default bookmarks
		bookmarks = bookmarkDao.query(bookmarkDao.queryBuilder().orderBy("sequence", true).prepare());
				
		if(bookmarks.size()==0){
			addBookmark("Naver", "http://m.naver.com", true);
			addBookmark("DongA", "http://m.donga.com", true);
			addBookmark("Daum", "http://m.daum.net", true);
			addBookmark("Otfried", "http://otfried-cheong.appspot.com/index.html", true);
			addBookmark("PLRG", "http://plrg.kaist.ac.kr", true);
			addBookmark("Google", "http://m.google.com", true);
			addBookmark("KAIST", "http://m.kaist.ac.kr", true);
			addBookmark("MIT", "http://m.mit.edu", true);
		}
	}
	
	public void setCurrentBookmarkPosition(int cur){
		currentPosition = cur;
	}
	
	public int getCurrentBookmarkPosition(){
		return currentPosition;
	}
	
	public void setBookmarkInterface(GetBookmarkInterface bmk){
		bmkChange = bmk;
	}

	public List<Bookmark> getBookmarks() throws SQLException {
		return bookmarks;
	}
	
	public void refreshLongClickStatus(){
		chosenBookmark = -1;
		lastTouchedTime = 0;
	}
	
	public void longClickHandler(int id){
		Date d = new Date();
		long now = d.getTime();
		
		if (chosenBookmark==-1){
			lastTouchedTime = now;
			chosenBookmark = id;
		}	
		//delete bookmark
		else if ((now - lastTouchedTime)<doublePressTimeInterval && chosenBookmark==id){
			try {
				//Log.i("bookmark", "delete bookmark"+id);
				deleteBookmark(chosenBookmark);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//reorder bookmark
		else if(chosenBookmark!=id){
			try {
				//Log.i("bookmark", "reorder bookmark"+id+","+chosenBookmark);
				reorderBookmark(chosenBookmark, id);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void addBookmark(String name, String url, boolean empty) throws NumberFormatException, SQLException{
		Bookmark bm = new Bookmark();
		bm.name = name;
		bm.url = url;
		
		//find current max sequence
		
		int seq = 0;
		
		if(bookmarks.size()>0){
			Iterator<Bookmark> iter = bookmarks.iterator();
			while(iter.hasNext()){
				seq = Math.max(seq, iter.next().sequence);
			}
		}
		
		bm.sequence = seq+1;
		
		bookmarkDao.create(bm);
								
		//update member var
		bookmarks.add(bm);
		if(!empty)
			bmkChange.onBookmarkChange();
	}
	
	public void deleteBookmark(int id) throws SQLException{
		Log.i("bookmark", "try delete"+id);
		Bookmark bm = bookmarkDao.queryForId(id);
		bookmarkDao.deleteById(id);
		bookmarks = bookmarkDao.query(bookmarkDao.queryBuilder().orderBy("sequence", true).prepare());
		app.getTextToSpeech().speak(String.format(app.getResources().getString(R.string.bookmarkDeleteMessage), bm.name), TextToSpeech.QUEUE_FLUSH, null);
		bmkChange.onBookmarkChange();
	}
	
	public void reorderBookmark(int id1, int id2) throws SQLException{
		Bookmark b1 = bookmarkDao.queryForId(id1);
		Bookmark b2 = bookmarkDao.queryForId(id2);
		int seq1 = b1.sequence;
		int seq2 = b2.sequence;
		b1.sequence = seq2;
		b2.sequence = seq1;
		bookmarkDao.update(b1);
		bookmarkDao.update(b2);
		bookmarks = bookmarkDao.query(bookmarkDao.queryBuilder().orderBy("sequence", true).prepare());
		app.getTextToSpeech().speak(app.getResources().getString(R.string.bookmarkChangeOrderMessage), TextToSpeech.QUEUE_ADD, null);
		bmkChange.onBookmarkChange();
		
	}

}
